#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QUdpSocket>
#include <QTcpSocket>
#include <QTimer>

namespace Ui {
class MainWindow;
}

enum
{
    FEET_ID=5,//1
    FEET_ReturnState=8,//1
    FEET_MinPosition=9,//2
    FEET_MaxPosition=11,//2
    FEET_Voltage=14,//1
    FEET_Deadzone=26,//2
    FEET_Offset=31,
    FEET_RunMode=33,
    FEET_TorqueEnable=40,
    FEET_GoalPosition=42,

};

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();


    QString readIPAddress();

private slots:
    void onBtnSend();
    void onBtnConnect();
    void onBtnClear();
    void onBtnAuto();
    void onBtnSearchIP();
    void onBtnSearchID();
    void onBtnSetID();
    void onBtnReadData();
    void onBtnWriteData();
    void onBtnRunMode();
    void onBtnSpeedMode();
    void onSliderInterval(int ms);
    void onSliderAngleT(int angle);
    void onSliderAngleG(int angle);
    void onSliderVelocity(int velocity);
    void autoSetAngle();
    void receiveUdpData();
    void newConnet();
    void receiveTcpData();
    void displayError(QAbstractSocket::SocketError);

    void timeoutCallback();
    void timeoutAutoMoveCallback();

private:
    void turnSide(uint8_t side);
    void readPositon();
    void torqueEnable(bool action);
    void lockEnble(bool action);
    void setRunMode(uint8_t mode);
    void setOffset(bool isInitial);
    void readPack(QByteArray data);
    void unpackData(uint8_t* data);

private:
    Ui::MainWindow *ui;
    QUdpSocket *udpSocket;
    QTcpSocket *tcpSocket;
    QTimer *m_timer;
    QTimer *m_autoMoveTimer;
    QString devName;
    QString devIPAddr;
    QHostAddress hostAddr;
    uint8_t timerState;
    uint16_t position;
    uint16_t minPosition;
    uint16_t maxPosition;
};

#endif // MAINWINDOW_H
